using System;
using RebelStar.Engine.GameView;

namespace RebelStar.Engine.Graphics.GDI
{
    public class GDIGraphicsDevice : IGameGraphicsDevice
    {
        readonly GameViewPanel control;
        private GDISurface surface;

        public GDIGraphicsDevice(GameViewPanel control)
        {
            this.control = control;
            this.control.Paint += control_Paint;
        }

        #region IGameGraphicsDevice Members
        public IServiceProvider ServiceProvider
        {
            get { return control; }
        }
        public GraphicDeviceMode Mode
        {
            get { return GraphicDeviceMode.GDI; }
        }
        public System.Drawing.Size ClientSize
        {
            get { return control.ClientSize; }
        }

        public void Clear(System.Drawing.Color color)
        {
            surface.Clear(color);
        }
        public void Redraw()
        {
            control.Invalidate();
        }
        public void Present()
        {
        }

        public ISurface CreateSurface()
        {
            surface = new GDISurface();
            return surface;
        }
        public ISurface CreateBuffer()
        {
            return new GDIBuffer(this.ClientSize);
        }

        public event EventHandler Paint;
        public event EventHandler ClientSizeChanged
        {
            add { control.ClientSizeChanged += value; }
            remove { control.ClientSizeChanged -= value; }
        }
        #endregion

        void control_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {

            if (surface != null)
                surface.Graphics = e.Graphics;
            if (Paint != null)
                Paint(this, EventArgs.Empty);
        }
    }
}
